﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2010, 2011
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Permissive License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion

using System;

namespace Smith.AMC.Operations
{
    /// <summary>
    /// Represents the Check And Set operation, i.e. Set this data in the cache, unless someone changed the value since the time I retrieved it.
    /// Remember to set the CAS Value
    /// </summary>
    public class CASOperation : Operation
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CASOperation"/> class.
        /// </summary>
        public CASOperation()
            : base(OperationNames.CheckAndSet)
        {
        }

        /// <summary>
        /// Gets or sets the cas value - only used if the Operation.Name is cas, i.e. check and set.
        /// </summary>
        public ulong CasValue
        {
            get;
            set;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (CasValue == 0)
            {
                throw new InvalidOperationException("CASValue not set, cannot continue");
            }

            if (string.IsNullOrEmpty(Key))
            {
                throw new InvalidOperationException("Key has not been set, cannot continue");
            }

            return string.Concat(Name, Space, Key, Space, Convert.ToString(Flags), Space, Convert.ToString(Expiry.TotalSeconds), Space, Convert.ToString(Data.Length), Space, Convert.ToString(CasValue), "\r\n");

        }

    }
}
